<?php

/**
 * Description of LocationController
 *
 * @author christiangreweling
 */
class LocationController extends Zend_Controller_Action {

    public function init() {
        $this->view->headScript()->appendFile('http://maps.google.com/maps/api/js?sensor=false');
        $this->_statusMessenger = $this->_helper->flashMessenger;
        $this->_errorMessenger = new Zend_Controller_Action_Helper_FlashMessenger();
        $this->_errorMessenger->setNamespace('error');
        $this->view->statusMessages = $this->_statusMessenger->getMessages();
    }

    /**
     * zeigt Eingabefeld und GoogleMaps karte
     * speichert Angebene Daten in Location Datenbank 
     */
    public function indexAction() {
        $this->view->headTitle($this->view->translate('Create new Location'));
        $this->view->documentClasses = array('location', 'index');


        $db = Zend_Db_Table::getDefaultAdapter();
        $select = new Zend_Db_Select($db);
        $select->from('location', array('id', 'name', 'lat', 'lang'));
        $resultSet = $db->fetchAll($select);

        $this->view->places = json_encode($resultSet);
    }

    public function addAction() {
        $this->view->headTitle($this->view->translate('Locations'));
        $this->view->documentClasses = array('location');


        $db = Zend_Db_Table::getDefaultAdapter();
        $select = new Zend_Db_Select($db);
        $select->from('location', array('id', 'name', 'lat', 'lang'));
        $resultSet = $db->fetchAll($select);

        $this->view->places = json_encode($resultSet);


        $this->view->form = $this->_getLocationForm();
        $post = $this->_request->getPost();

        if ($post) {
            if (!$this->view->form->isValid($post)) {
                $this->view->form->populate($post);
            } else {
                $values = $this->_request->getParams();

                $location = new Application_Model_LocationModel($values);
                $locationMapper = new Application_Model_LocationMapper();
                $locationMapper->save($location);

                if (null !== $location->id) {
                    $this->_statusMessenger->addMessage(
                            $this->view->translate('Location saved.')
                    );

                    $this->_redirect('event/add-event');
                } else {
                    $this->_errorMessenger->addMessage(
                            $this->view->translate('Error while saving.')
                    );
                }
            }
        }
    }

    /**
     * erstellt Form für indexAction 
     * 
     * @return \Zend_Form 
     */
    protected function _getLocationForm() {
        $form = new Zend_Form();
        $form->setMethod(Zend_Form::METHOD_POST)
                ->addElements(array(
                    array('text', 'name', array(
                            'required' => true,
                            'label' => _('Name:'),
                            'size' => 45,
                            'placeholder' => $this->view->translate('Enter a fitting name'),
                            'required' => true,
                            'validators' => array(
                                array('regex', false, '/^[a-zA-Z0-9_ ÖöÄäÜü]+$/'),
                                array('StringLength', false, 4)
                            )
                    )),
                    array('submit', 'submit', array(
                            'label' => _('Create'),
                    )),
                    array('hidden', 'lat', array(
                            'required' => true,
                    )),
                    array('hidden', 'lang', array(
                            'required' => true,
                    )),
                ));
        $form->setDecorators(array('Viewhelper'));
        $form->removeDecorator('Label');
        $form->removeDecorator('HtmlTag');
        $form->setElementDecorators(array('ViewHelper', 'Errors'));
        return $form;
    }

}

?>
